const Koa=require('koa');
const Router=require('koa-router');
const app=new Koa();
const router=new Router();
const static=require('koa-static');
app.use(static(__dirname+'/views'));
const parser=require('koa-bodyparser');
app.use(parser());
const {exec}=require('./db');

//注册接口
router.post('/register',async(ctx)=>{
    const {name,psw}=ctx.request.body;
    const data=await exec(`select * from user_work where userName='${name}'`);
    if(data.length>0){
        ctx.body={
            message:'该用户已存在',
            code:1
        } 
    }else{
        await exec(`insert into user_work  (userName,passWord) values ('${name}','${psw}')`)
        ctx.body={
            message:'注册成功',
            code:0
        }
    }
   
});

//登录接口
router.post('/login',async(ctx)=>{
   const {name,psw}=ctx.request.body; 
   const data=await exec(`select * from user_work where userName='${name}'and passWord='${psw}'`);
   if(data.length>0){
    ctx.body={
        message:'登录成功',
        code:0
    }
   }else{
        ctx.body={
            message:'请先注册',
            code:0
        }
   }
    
})
app.use(router.routes());
app.listen(3000,()=>{
    console.log('服务端已启动http://127.0.0.1:3000');
})